package com.wolfalpha.lwtool.activity;

import java.util.List;
import com.wolfalpha.lwtool.R;
import com.wolfalpha.lwtool.adapter.SouAdapter;
import com.wolfalpha.lwtool.constant.Application;
import com.wolfalpha.lwtool.vo.DB;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class HistoryActivity extends Activity implements OnItemClickListener {

	private InfoTask mTask;
	private ProgressDialog pd;
	private TextView hnum;
	private ListView hlist;
	private List<DB> dbs;
	private SouAdapter sdapter;
	private StringBuffer jsonData = new StringBuffer();;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_history);
		// 设置当前活动状态
		Application.setFrontActivity(Application.N_HISTORY);

		dbs = Application.getDao(this).query();
		initView();
		initListeners();
	}

	@Override
	public void onResume(){
		super.onResume();
		if(jsonData.length()>0)jsonData.delete(0, jsonData.length()-1);
	}
	
	private void initListeners() {
		hlist.setOnItemClickListener(this);
	}

	private void initView() {
		hnum = (TextView) findViewById(R.id.hnum);
		hnum.setText(String.valueOf(dbs.size()));
		hlist = (ListView) findViewById(R.id.hlist);
		sdapter = new SouAdapter(this, dbs);
		hlist.setAdapter(sdapter);
		pd = new ProgressDialog(this);
	}

	private class InfoTask extends AsyncTask<String, Integer, String> {
		// onPreExecute方法用于在执行后台任务前做一些UI操作
		@Override
		protected void onPreExecute() {
			pd.setMessage("正在接收JSON数据");
			pd.setCancelable(false);
			pd.show();
		}

		// doInBackground方法内部执行后台任务,不可在此方法内修改UI
		@Override
		protected String doInBackground(String... params) {
			try {
				System.out.println(params[0]);
				jsonData.append(Application
						.getBaseService(HistoryActivity.this).getJsonById(
								params[0]));
			} catch (Exception e) {
				e.printStackTrace();
				this.cancel(true);
			}
			return null;
		}

		// onProgressUpdate方法用于更新进度信息
		@Override
		protected void onProgressUpdate(Integer... progresses) {
		}

		// onPostExecute方法用于在执行完后台任务后更新UI,显示结果
		@Override
		protected void onPostExecute(String result) {
			String tem = jsonData.toString();
			tem = tem.replace(",\"", "\n\"");
			
			Intent intent = new Intent();
			intent.setClass(HistoryActivity.this, InfoActivity.class);
			Bundle bundle = new Bundle();
			bundle.putString("json", tem);
			intent.putExtras(bundle);
			startActivity(intent);
			pd.dismiss();
		}

		// onCancelled方法用于在取消执行中的任务时更改UI
		@Override
		protected void onCancelled() {
			Toast.makeText(HistoryActivity.this, "获取失败，请检查网络",
					Toast.LENGTH_SHORT).show();
			pd.dismiss();
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		mTask = new InfoTask();
		mTask.execute(String.valueOf(dbs.get(arg2).getId()));
	}
}
